package com.brainwave.models;

import androidx.room.Entity;
import androidx.room.PrimaryKey;

import java.util.Date;

/**
 * 脑波数据模型类
 * 用于存储从NeuroSky设备接收到的各种脑波数据
 */
@Entity(tableName = "brainwave_data")
public class BrainWaveData {
    
    @PrimaryKey(autoGenerate = true)
    private long id;
    
    // 时间戳
    private long timestamp;
    private Date recordTime;
    
    // 信号质量 (0-200, 0表示信号最好)
    private int poorSignal;
    
    // eSense值 (0-100)
    private int attention;      // 注意力
    private int meditation;     // 冥想
    
    // 眨眼强度 (0-255)
    private int blinkStrength;
    
    // 原始脑波数据 (-32768 to 32767)
    private int rawValue;
    
    // EEG功率谱数据 (各频段的功率值)
    private int delta;          // 0.5-2.75Hz (深度睡眠)
    private int theta;          // 3.5-6.75Hz (浅睡眠、冥想)
    private int lowAlpha;       // 7.5-9.25Hz (放松状态)
    private int highAlpha;      // 10-11.75Hz (清醒放松)
    private int lowBeta;        // 13-16.75Hz (轻度集中)
    private int highBeta;       // 18-29.75Hz (高度集中)
    private int lowGamma;       // 31-39.75Hz (认知处理)
    private int highGamma;      // 41-49.75Hz (高级认知)
    
    // 会话信息
    private String sessionId;
    private String deviceName;
    private String deviceAddress;
    
    // 数据状态
    private boolean isValid;
    private String dataSource;
    
    /**
     * 默认构造函数
     */
    public BrainWaveData() {
        this.timestamp = System.currentTimeMillis();
        this.recordTime = new Date();
        this.isValid = true;
        this.dataSource = "NeuroSky";
    }
    
    /**
     * 构造函数 - 基础数据
     */
    public BrainWaveData(int poorSignal, int attention, int meditation) {
        this();
        this.poorSignal = poorSignal;
        this.attention = attention;
        this.meditation = meditation;
    }
    
    /**
     * 构造函数 - 完整数据
     */
    public BrainWaveData(int poorSignal, int attention, int meditation, int blinkStrength,
                        int rawValue, int delta, int theta, int lowAlpha, int highAlpha,
                        int lowBeta, int highBeta, int lowGamma, int highGamma) {
        this();
        this.poorSignal = poorSignal;
        this.attention = attention;
        this.meditation = meditation;
        this.blinkStrength = blinkStrength;
        this.rawValue = rawValue;
        this.delta = delta;
        this.theta = theta;
        this.lowAlpha = lowAlpha;
        this.highAlpha = highAlpha;
        this.lowBeta = lowBeta;
        this.highBeta = highBeta;
        this.lowGamma = lowGamma;
        this.highGamma = highGamma;
    }
    
    // Getters and Setters
    
    public long getId() {
        return id;
    }
    
    public void setId(long id) {
        this.id = id;
    }
    
    public long getTimestamp() {
        return timestamp;
    }
    
    public void setTimestamp(long timestamp) {
        this.timestamp = timestamp;
        this.recordTime = new Date(timestamp);
    }
    
    public Date getRecordTime() {
        return recordTime;
    }
    
    public void setRecordTime(Date recordTime) {
        this.recordTime = recordTime;
        this.timestamp = recordTime.getTime();
    }
    
    public int getPoorSignal() {
        return poorSignal;
    }
    
    public void setPoorSignal(int poorSignal) {
        this.poorSignal = poorSignal;
    }
    
    public int getAttention() {
        return attention;
    }
    
    public void setAttention(int attention) {
        this.attention = attention;
    }
    
    public int getMeditation() {
        return meditation;
    }
    
    public void setMeditation(int meditation) {
        this.meditation = meditation;
    }
    
    public int getBlinkStrength() {
        return blinkStrength;
    }
    
    public void setBlinkStrength(int blinkStrength) {
        this.blinkStrength = blinkStrength;
    }
    
    public int getRawValue() {
        return rawValue;
    }
    
    public void setRawValue(int rawValue) {
        this.rawValue = rawValue;
    }
    
    public int getDelta() {
        return delta;
    }
    
    public void setDelta(int delta) {
        this.delta = delta;
    }
    
    public int getTheta() {
        return theta;
    }
    
    public void setTheta(int theta) {
        this.theta = theta;
    }
    
    public int getLowAlpha() {
        return lowAlpha;
    }
    
    public void setLowAlpha(int lowAlpha) {
        this.lowAlpha = lowAlpha;
    }
    
    public int getHighAlpha() {
        return highAlpha;
    }
    
    public void setHighAlpha(int highAlpha) {
        this.highAlpha = highAlpha;
    }
    
    public int getLowBeta() {
        return lowBeta;
    }
    
    public void setLowBeta(int lowBeta) {
        this.lowBeta = lowBeta;
    }
    
    public int getHighBeta() {
        return highBeta;
    }
    
    public void setHighBeta(int highBeta) {
        this.highBeta = highBeta;
    }
    
    public int getLowGamma() {
        return lowGamma;
    }
    
    public void setLowGamma(int lowGamma) {
        this.lowGamma = lowGamma;
    }
    
    public int getHighGamma() {
        return highGamma;
    }
    
    public void setHighGamma(int highGamma) {
        this.highGamma = highGamma;
    }
    
    public String getSessionId() {
        return sessionId;
    }
    
    public void setSessionId(String sessionId) {
        this.sessionId = sessionId;
    }
    
    public String getDeviceName() {
        return deviceName;
    }
    
    public void setDeviceName(String deviceName) {
        this.deviceName = deviceName;
    }
    
    public String getDeviceAddress() {
        return deviceAddress;
    }
    
    public void setDeviceAddress(String deviceAddress) {
        this.deviceAddress = deviceAddress;
    }
    
    public boolean isValid() {
        return isValid;
    }
    
    public void setValid(boolean valid) {
        isValid = valid;
    }
    
    public String getDataSource() {
        return dataSource;
    }
    
    public void setDataSource(String dataSource) {
        this.dataSource = dataSource;
    }
    
    /**
     * 检查信号质量是否良好
     */
    public boolean hasGoodSignal() {
        return poorSignal < 50; // 信号质量阈值
    }
    
    /**
     * 获取总Alpha波功率
     */
    public int getTotalAlpha() {
        return lowAlpha + highAlpha;
    }
    
    /**
     * 获取总Beta波功率
     */
    public int getTotalBeta() {
        return lowBeta + highBeta;
    }
    
    /**
     * 获取总Gamma波功率
     */
    public int getTotalGamma() {
        return lowGamma + highGamma;
    }
    
    /**
     * 计算放松指数 (基于Alpha和Theta波)
     */
    public double getRelaxationIndex() {
        int totalPower = delta + theta + getTotalAlpha() + getTotalBeta() + getTotalGamma();
        if (totalPower == 0) return 0.0;
        return (double) (theta + getTotalAlpha()) / totalPower * 100;
    }
    
    /**
     * 计算专注指数 (基于Beta波)
     */
    public double getFocusIndex() {
        int totalPower = delta + theta + getTotalAlpha() + getTotalBeta() + getTotalGamma();
        if (totalPower == 0) return 0.0;
        return (double) getTotalBeta() / totalPower * 100;
    }
    
    /**
     * 获取信号质量描述
     */
    public String getSignalQualityDescription() {
        if (poorSignal == 0) return "优秀";
        else if (poorSignal < 25) return "良好";
        else if (poorSignal < 50) return "一般";
        else if (poorSignal < 100) return "较差";
        else return "很差";
    }
    
    /**
     * 获取注意力等级描述
     */
    public String getAttentionLevelDescription() {
        if (attention >= 80) return "非常专注";
        else if (attention >= 60) return "专注";
        else if (attention >= 40) return "一般";
        else if (attention >= 20) return "分散";
        else return "非常分散";
    }
    
    /**
     * 获取冥想等级描述
     */
    public String getMeditationLevelDescription() {
        if (meditation >= 80) return "深度冥想";
        else if (meditation >= 60) return "冥想";
        else if (meditation >= 40) return "放松";
        else if (meditation >= 20) return "轻微放松";
        else return "紧张";
    }
    
    @Override
    public String toString() {
        return String.format("BrainWaveData{id=%d, timestamp=%d, poorSignal=%d, " +
                "attention=%d, meditation=%d, rawValue=%d, sessionId='%s'}",
                id, timestamp, poorSignal, attention, meditation, rawValue, sessionId);
    }
    
    @Override
    public boolean equals(Object obj) {
        if (this == obj) return true;
        if (obj == null || getClass() != obj.getClass()) return false;
        
        BrainWaveData that = (BrainWaveData) obj;
        return id == that.id && timestamp == that.timestamp;
    }
    
    @Override
    public int hashCode() {
        return Long.hashCode(id) * 31 + Long.hashCode(timestamp);
    }
}